package presenters;

import java.sql.ResultSet;
import java.sql.SQLException;

import model.ConnectionManager;
import model.IPresenter;
import model.Model;
import views.LoginBoxPanel;

public class LoginPresenter extends IPresenter{
	private LoginBoxPanel myPanel;
	
	public LoginPresenter(LoginBoxPanel p) {
		super();
		myPanel = p;
	}
	
	
	public int login(String username, String password) {
		int loginResult = attemptLogin(username, password);
		
		switch(loginResult) {
		case 0:
			// Login as MEMBER, take to Personal Information screen
			Model.setCurrentUser(username);
			Model.setCurrentUserType("MEMBER");
			System.out.println("Log in Member");
			return 1;
		case -1:
			System.out.println("Error in Login Attempt: " + -1);
			return 0;
		case 1:
			// Login as ADMINISTRATION
			Model.setCurrentUser(username);
			Model.setCurrentUserType("ADMINISTRATOR");
			System.out.println("Log in Admin");
			return 1;
		case 2:
			// Login as EMPLOYEE
			Model.setCurrentUser(username);
			Model.setCurrentUserType("EMPLOYEE");
			System.out.println("Log in Employee");
			return 1;
		case 3:
			// Login as MEMBER
			Model.setCurrentUser(username);
			Model.setCurrentUserType("MEMBER");
			System.out.println("Log in Member");
			return 1;
		default:
			System.out.println("Error in Login Attempt:" + loginResult);
			return 0;
		}
	}
	
	/**
	 * Attempts to log the user into the system
	 * @param username Name of the user
	 * @param password Password for the user
	 * @return The type of user logging in or -1 if unsuccessful.
	 *     1 = ADMINISTRATION
	 *     2 = EMPLOYEE
	 *     3 = MEMBER
	 */
	private int attemptLogin(String username, String password) {
		System.out.println("Attempt login for user: " + username + "\tpassword: " + password);
		int out = -1;
		
		if(checkString(username) && checkString(password)) {
			/*IF the following table results null then invalid, else log in */
			String sql1 = "SELECT COUNT(*) "
					+ "FROM "+ ConnectionManager.GROUP +".USER " 
					+ "WHERE USERNAME = '" 
					+ username + "' AND PASSWORD = '"
					+ password + "' GROUP BY USERNAME;";

			//CHECK FOR WHICH ONE OF THESE THREE IS NON NULL FOR WHICH WELCOME SCREEN TO SHOW
			String sql2 = 
			"SELECT COUNT(*) "
			+ "FROM ADMINISTRATOR " 
			+ "WHERE USERNAME = '" + username + "' GROUP BY USERNAME;";

			String sql3 = 
			"SELECT COUNT(*) "
			+ "FROM EMPLOYEE "
			+ "WHERE USERNAME = '" + username + "' GROUP BY USERNAME;";
			
			String sql4 = 
			"SELECT COUNT(*) "
			+ "FROM MEMBER "
			+ "WHERE USERNAME = '" + username + "' GROUP BY USERNAME;";
			
			
			try {
				ResultSet rs1 = executeSQLQuery(sql1);
				ResultSet rs2 = executeSQLQuery(sql2);
				ResultSet rs3 = executeSQLQuery(sql3);
				ResultSet rs4 = executeSQLQuery(sql4);
				
				
				// Checks 
				if (rs1.first()) {
					out = 0;
				} else {
					out = -1;
				}
				
				if(rs2.first()) {
					out = 1;
				}
				else if(rs3.first()) {
					out = 2;
				} else if(rs4.first()) {
					out = 3;
				}

			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} // close catch
		} // close if
				
		return out;
	} // close attemptLogin
} // close LoginPresenter class
